<template>
  <el-container class="order-page panel-block">
    <el-header class="sa-header">
      <el-tabs class="sa-tabs" v-model="filterParams.data.status" @tab-change="onChangeStatus">
        <el-tab-pane
          v-for="(sl, key) in statusList"
          :key="key"
          :label="sl.name"
          :name="sl.type"
        ></el-tab-pane>
      </el-tabs>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">团购订单</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button
            class="sa-button-refresh"
            icon="RefreshRight"
            @click="getData()"
          ></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <el-table
        height="100%"
        class="sa-table"
        :data="table.data"
      >
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column label="商品信息" align="center" min-width="300">
          <template #default="scope">
            <div class="goods-item">
              <el-image class="goods-image" :src="checkUrl(scope.row.goods_image)" :fit="'cover'" />
              <div class="goods-text">
                <div>{{ scope.row.goods_title }}</div>
                <div>￥{{ scope.row.goods_original_price }} x{{ scope.row.goods_num }}
                  优惠￥{{ Number(scope.row.goods_original_price) - Number(scope.row.goods_price) }}
                  实付￥{{ scope.row.goods_price }}
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="已使用/购买数量" align="center" min-width="140">
          <template #default="scope">
            <div class="sa-flex sa-row-center">
              <div>{{ scope.row.group_ticket_count }}/{{ scope.row.goods_num }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单编号" prop="order_sn" align="center" min-width="260" show-overflow-tooltip />
        <el-table-column label="下单用户" align="center" min-width="140">
          <template #default="scope">
            <sa-user-profile :user="{
              nickname:scope.row.nickname
            }" :id="scope.row.user_id" mode="col" />
          </template>
        </el-table-column>
        <el-table-column label="用户电话" prop="mobile" align="center" min-width="120" />
        <el-table-column label="店铺名称" prop="name" align="center" min-width="180" show-overflow-tooltip />
        <el-table-column label="下单时间" prop="create_time" align="center" min-width="180" show-overflow-tooltip />
        <el-table-column label="到期时间" prop="validity_time" align="center" min-width="180" show-overflow-tooltip />
        <el-table-column label="订单状态" prop="status_text" align="center" min-width="142" />
        <el-table-column label="操作" fixed="right" min-width="150">
          <template #default="scope">
            <el-button class="is-link" v-auth="'seller.admin.grouporder.details'" type="primary"
                       @click="lookInfo(scope.row)">详情
            </el-button>
            <el-button class="is-link" v-auth="'seller.admin.grouporder.check'" type="primary"
                       @click="check(scope.row)">核销
            </el-button>
            <el-button class="is-link" v-auth="'seller.admin.grouporder.record'" type="primary"
                       @click="lookRecord(scope.row)">记录
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script>
  export default {
    name: 'seller.admin.grouporder',
  };
</script>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from './groupOrder.service';
  import { usePagination, useModal, useSearch } from '@/sheep/hooks';
  import { checkUrl } from '@/sheep/utils/checkUrlSuffix';
  import { useRoute } from 'vue-router';
  import details from './details.vue';
  import verification from './verification.vue';
  import record from './record.vue';
  import { cloneDeep } from 'lodash';

  const route = useRoute();

  const statusList = reactive([
    { name: '全部', type: '' },
    { name: '待付款', type: 'unpaid' },
    { name: '待使用', type: 'unused' },
    { name: '已取消', type: 'cancel' },
    { name: '已完成', type: 'completed' },
  ]);

  const loading = ref(true);

  const table = reactive({
    data: [],
    status: '',
  });

  const filterParams = reactive({
    tools: {
      user: {
        type: 'tinputprepend',
        placeholder: '请输入查询内容',
        field: 'user',
        user: {
          field: 'nickname',
          value: '',
        },
        options: [
          {
            label: '用户昵称',
            value: 'nickname',
          },
          {
            label: '手机号',
            value: 'mobile',
          },
        ],
      },
      order_sn: {
        type: 'tinput',
        field: 'order_sn',
        value: '',
        label: '订单编号',
        placeholder: '请输入查询内容',
      },
      goods_title: {
        type: 'tinput',
        field: 'goods_title',
        value: '',
        label: '商品名称',
        placeholder: '请输入查询内容',
      },
      name: {
        type: 'tinput',
        field: 'name',
        value: '',
        label: '商品名称',
        placeholder: '请输入查询内容',
      },
      status: {
        type: 'tselect',
        label: '订单状态',
        field: 'status',
        value: '',
        options: {
          data: [
            { label: '全部', value: '' },
            { label: '待付款', value: 'unpaid' },
            { label: '待使用', value: 'unused' },
            { label: '已取消', value: 'cancel' },
            { label: '已完成', value: 'completed' },
          ],
        },
      },
      time: {
        type: 'tdatetimerange',
        label: '下单时间',
        field: 'time',
        value: [],
      },
    },
    data: {
      order_sn: '',
      goods_title: '',
      name: '',
      status: '',
      time: [],
      user: { field: 'nickname', value: '' },
    },
    conditionLabel: {},
  });

  onMounted(async () => {
    await getData();
  });

  const lookRecord = (row) => {
    useModal(
      record,
      {
        title: '记录',
        type: 'record',
        id: row.id,
      },
      {},
    );
  };

  const lookInfo = (row) => {
    useModal(
      details,
      {
        title: '订单详情',
        type: 'details',
        id: row.id,
      },
      {},
    );
  };

  const check = (row) => {
    useModal(
      verification,
      {
        title: '核销',
        type: 'check',
        id: row.id,
      },
      () => {
        getData(1);
      },
    );
  };

  const { pageData } = usePagination();

  // 获取数据
  async function getData(page) {
    loading.value = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    const { error, data } = await api.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      order_sn: tempSearch.order_sn,
      goods_title: tempSearch.goods_title,
      nickname: tempSearch.user.field === 'nickname' ? tempSearch.user.value : '',
      mobile: tempSearch.user.field === 'mobile' ? tempSearch.user.value : '',
      start_time: tempSearch.time[0] || '',
      end_time: tempSearch.time[1] || '',
      status: tempSearch.status,
      name: tempSearch.name,
    });
    if (error === 0) {
      table.data = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    loading.value = false;
  }

  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });

  function onChangeStatus(tab) {
    table.data = [];
    getData(1);
  }
</script>
<style lang="scss" scoped>


  .goods-item {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .goods-image {
      width: 58px;
      height: 58px;
      border-radius: 4px;
      margin-right: 10px;
    }

    .goods-text {
      height: 58px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-start;
      font-size: 12px;
    }
  }
</style>
